package com.aaa.tars.controller;

import com.aaa.tars.entity.Guides;
import com.aaa.tars.entity.guideImages;
import com.aaa.tars.service.GuidesService;
import com.aaa.tars.util.Page;
import com.aaa.tars.util.Result;
import com.aaa.tars.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @FileName: GuidesController
 * @Description: 攻略控制层
 * @Author: 李宗盛
 * @CreateTime: 2025/9/11 16:40
 * @Version: 17
 */
@RestController
@RequestMapping("/guides")
public class GuidesController {
    @Autowired
    private GuidesService guidesService;
    
    @PostMapping("queryByPage")
    public Result queryByPage(@RequestBody Page<Guides> page) {
        return ResultUtil.success(guidesService.queryByPage(page));
    }
    
    @GetMapping("queryById")
    public Result queryById(Integer guideId) {
        return ResultUtil.success(guidesService.queryById(guideId));
    }

    @PostMapping("add")
    public Result add(@RequestBody Guides guides) {
        try {
            // 先保存攻略基本信息
            int result = guidesService.add(guides);

            if (result > 0) {
                // 返回新插入的攻略ID
                return ResultUtil.success(guides.getGuideId());
            }
            return ResultUtil.error("添加失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("系统错误: " + e.getMessage());
        }
    }
    
    @PostMapping("update")
    public Result update(@RequestBody Guides guides) {
        return ResultUtil.success(guidesService.update(guides));
    }
    
    @DeleteMapping("deleteById")
    public Result deleteById(Integer guideId) {
        return ResultUtil.success(guidesService.deleteById(guideId));
    }
    /**
     * 上传图片
     * @param file
     * @return
     */
    @PostMapping("/uploadImage")
    public Result uploadImage(@RequestParam("file") MultipartFile file) {
        return guidesService.uploadImage(file);
    }
}